import os
import logging

from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import login_required

from listmodel import ListModel
import utils

class ViewListRequestHandler(webapp.RequestHandler):
    @login_required
    def get(self, key_id_in_url):
        # Check that the ID was passed in the URL
        if (key_id_in_url):
            # Getting the List
            try:
               list = ListModel.get_by_id(int(key_id_in_url))
            except:
               self.redirect('/')
               return
               
            # Check this User has rights to see this List
            if (not utils.isListVisibileToUser(list, users.get_current_user())):
                self.redirect('/')
                return
            
            # Preparing the Template Input Values
            template_input_values = {
                                     'list_title' : list.title,
                                     'description' : list.description,
                                     'tags' : list.tags,
                                     'completion_date' : list.completion_date,
                                     'creation_date' : list.creation_date,
                                     'due_date' : list.due_date,
                                     'visibility' : list.visibility
                                     }
            utils.fillLoginbarTemplateInputValues(self, template_input_values)
            
            # Rendering the Template
            template_path = os.path.join(os.path.dirname(__file__), 'templates/list.html')
            self.response.out.write(template.render(template_path, template_input_values))
        else:
            # No Title was given, so redirect to the Main page
            self.redirect('/')
            return